<?php if ( ! defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );
/**
 * BackendPro
 *
 * A website backend system for developers for PHP 4.3.2 or newer
 *
 * @package         conlabz CMS
 * @author          Christian Schneider
 * @copyright       Copyright (c) 2009
 * @license        
 * @link            http://www.conlabz.de
 * @filesource
 */

// ---------------------------------------------------------------------------

/**
 * Categories
 *
 * @package         conlabz CMS
 * @subpackage      Controllers
 */
class Categories extends User_Controller {
	function Categories( ) {
		parent::User_Controller( );
		
		$this->load->helper( 'form' );

		// Check for access permission
		// needs to be defined as a resource over the admin area
		check( 'Events' );

		// Load the validation library
		$this->load->library( 'validation' );

		// Load the models
		$this->load->model( 'categories_model' );
		$this->load->module_model( 'article', 'website_model' );
		$this->load->module_model( "article", "category_model" );
		
		// Load the events module
		$this->lang->module_load( 'events', 'events' );

		// Set breadcrumb
		$this->bep_site->set_crumb( $this->lang->line( 'events' ), 'events/user/categories' );
				
		log_message( 'debug', 'conlabz CMS : Events class loaded' );
	}	
	
	/**
	 * View Events
	 *
	 * @access public
	 */
	function index( ) {
		// Get events
		$data['categories']   = $this->categories_model->getCategories( );
		
		// Display Page
		$data['header'] = $this->lang->line( 'event_categories_overview' );
		$data['page']   = $this->config->item( 'backendpro_template_user' ) . "/view_categories";
		$data['menu'] = $this->website_model->getWebsiteUserMenu( );
		$data['module'] = 'events';
		
		$this->load->module_view( 'article', 'user/container', $data );
	}
		
	/**
	 * Get Category Details
	 *
	 * Load blog detail values from the submitted form
	 *
	 * @access private
	 * @return array
	 */
	function _get_category_details( ) {
		$data['id']          = $this->input->post( 'id'          );
		$data['title']       = $this->input->post( 'title'       );
		$data['description'] = $this->input->post( 'description' );
		$data['category_id'] = $this->input->post( 'category_id' );
		
		return $data;
	}
	
	/**
	 * Display Category Form
	 *
	 * @access public
	 * @param integer $id Event ID
	 */
	function form( $id = NULL ) {
		// VALIDATION FIELDS
		$fields['id']          = "ID";
		$fields['title']       = $this->lang->line( 'event_categories_name'        );
		$fields['category_id'] = $this->lang->line( 'event_category'               );
		$fields['description'] = $this->lang->line( 'event_categories_description' );
		
		$this->validation->set_fields( $fields );

		// Setup validation rules
		if( is_null( $id ) ) {
			// Use create website rules (make sure no-one has the same short name)
			$rules['title']       = "trim|required";
			$rules['description'] = "trim";
			$rules['category_id'] = "required";
		} else {                                  
			// Use edit website rules (make sure no-one other than the current user has the same short name)
			$rules['title']       = "trim|required";
			$rules['description'] = "trim";
			$rules['category_id'] = "required";
		}
		
		// Setup form default values
		if( !is_null( $id ) AND !$this->input->post( 'submit' ) ) {
			// Modify form, first load
			$categories = $this->categories_model->getCategories( array( 'event_type.id' => $id ) );
			$categories = $categories->row_array( );
			$this->validation->set_default_value( 'categories', $categories['id'] );
			$this->validation->set_default_value( $categories );
		} elseif( is_null( $id ) AND !$this->input->post( 'submit' ) ) {
			// Create form, first load
			$this->validation->set_default_value( 'category_id', $this->preference->item( 'default_categories_category' ) );
		} elseif( $this->input->post( 'submit' ) ) {
			// Form submitted, check rules
			$this->validation->set_rules( $rules );
		}

		// RUN
		if( $this->validation->run( ) === FALSE) {
			// Construct Category dropdown
			$website = " AND website_id = " . $this->session->userdata( 'website_id' );
			$data['category_id'] = $this->category_model->buildCategoryDropdown( 'category', 'id', $website );
			
			$this->validation->output_errors( );
			$data['fckeditor'] = array(
              	'name'        => 'description',
              	'id'          => 'description',
              	'toolbarset'  => 'Default',
              	'basepath'    => DEVPATH.'assets/plugins/fckeditor/',
              	'width'       => '60%',
              	'height'      => '300',
				'config'	  => '/template/'.$this->session->userdata( 'template' ).'/js/fckeditor.js'
    		);
			
			$this->validation->output_errors( );
			$data['header'] = ( is_null( $id ) ? $this->lang->line( 'create_event_category' ) : $this->lang->line( 'edit_event_category' ) );
			$this->bep_site->set_crumb( $data['header'], 'events/user/categories/form/'.$id );
			$data['page']   = $this->config->item( 'backendpro_template_user' ) . "/form_events_categories";
			$data['menu'] = $this->website_model->getWebsiteUserMenu( );
			$data['module'] = 'events';
			$this->load->module_view( 'article', 'user/container', $data );
		} else {
			// Save form
			if( is_null( $id ) ) {
				// CREATE
				// Fetch form values
				$categories = $this->_get_category_details( );
				$categories['timestamp'] = date( 'Y-m-d H:i:s' );
			
				$this->db->trans_start( );
				$categories['event_id'] = $this->categories_model->insert( $categories );
				
				if( $this->db->trans_status( ) === TRUE ) {
					$this->db->trans_commit( );
					flashMsg( 'success', sprintf( $this->lang->line( 'userlib_user_saved' ), $categories['title'] ) );
				} else {
					$this->db->trans_rollback( );
					flashMsg( 'error', sprintf( $this->lang->line( 'backendpro_action_failed' ), $this->lang->line( 'userlib_create_user' ) ) );
				}
				redirect( 'events/user/categories' );
			} else {
				// SAVE
				$categories = $this->_get_category_details( );
				$categories['timestamp'] = date( 'Y-m-d H:i:s' );
				
				$this->db->trans_start( );
				$this->categories_model->update( $categories, $categories['id'], 'categories' );

				if( $this->db->trans_status( ) === TRUE ) {
					$this->db->trans_commit( );
					flashMsg( 'success', sprintf( $this->lang->line( 'userlib_user_saved' ), $categories['title'] ) );
				} else {
					$this->db->trans_rollback( );
					flashMsg( 'error', sprintf( $this->lang->line( 'backendpro_action_failed' ), $this->lang->line( 'userlib_edit_user' ) ) );
				}
				redirect( 'events/user/categories' );
			}
		}
	} // form( );

	/**
	 * Delete
	 *
	 * Delete the selected events from the system
	 *
	 * @access public
	 */
	function delete( ) {
		if( FALSE === ( $selected = $this->input->post( 'select' ) ) ) {
			redirect( 'events/user/categories', 'categories' );
		}

		foreach( $selected as $categories ) {
			$this->db->delete( 'event_type', array( 'id' => $categories ) );
		}

		flashMsg( 'success', $this->lang->line( 'event_delete_confirm' ) );
		redirect( 'events/user/categories', 'categories' );
	}
}
/* End of file categories.php */
/* Location: ./modules/events/controllers/user/categories.php */